package com.autumn.mybatis.session;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.defaults.DefaultSqlSession;

/**
 * 上下文会话
 * 
 * @author 杨昌国
 *
 *         2017-10-19 12:27:36
 */
public class AutumnSqlSession extends DefaultSqlSession {

	private AutumnSqlSessionFactory factory;

	/**
	 * 实例化 ContextSession 新实例
	 * 
	 * @param factory
	 *            工厂
	 * @param configuration
	 *            配置
	 * @param executor
	 *            执行器
	 */
	public AutumnSqlSession(AutumnSqlSessionFactory factory, Configuration configuration, Executor executor) {
		this(factory, configuration, executor, false);
	}

	/**
	 * 实例化 ContextSession 新实例
	 * 
	 * @param factory
	 *            工厂
	 * @param configuration
	 *            配置
	 * @param executor
	 *            执行器
	 * @param autoCommit
	 *            自动提交
	 */
	public AutumnSqlSession(AutumnSqlSessionFactory factory, Configuration configuration, Executor executor,
			boolean autoCommit) {
		super(configuration, executor, autoCommit);
		this.factory = factory;
		if (this.factory != null) {
			this.factory.setCurrnetSession(this);
		}
	}

	/**
	 * 关闭
	 */
	@Override
	public void close() {
		try {
			super.close();
		} finally {
			if (this.factory != null) {
				this.factory.removeCurrnetSession();
				this.factory = null;
			}
		}
	}

}
